﻿using JUtil;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SqlConnectionSettings
{
    public partial class GridViewSettings : Form
    {
        public GridViewSettings()
        {
            InitializeComponent();

            theDatabaseMode.Text = DatabaseMode.Oracle.ToString();
            theConnectionString.Text = "Data Source=tppddb;User ID=tpdbuser;Password=tpdbuser;";
            tbSelectSQL.Text = "select * from FESTIVAL_ACTIVITY";
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            DatabaseMode mode = (DatabaseMode)Enum.Parse(typeof(DatabaseMode), theDatabaseMode.Text);
            string ConnectionString = theConnectionString.Text;
            SqlConnectionSettings dlg = new SqlConnectionSettings(mode, ConnectionString);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                theDatabaseMode.Text = dlg.GetDatabaseMode().ToString();
                theConnectionString.Text = dlg.GetConnectionString();
            }
        }

        private void btnGetFields_Click(object sender, EventArgs e)
        {
            DatabaseMode mode = (DatabaseMode)Enum.Parse(typeof(DatabaseMode), theDatabaseMode.Text);
            string connectionString = theConnectionString.Text;
            string selectSQL = tbSelectSQL.Text;

            IDatabase db = new XDatabase(mode, connectionString);

            List<FieldSchema> lstFields = FieldSchema.GetTableSchema(db, selectSQL);

            

            DataTable dt = new DataTable();

            DataColumn col = null;

            col = new DataColumn();
            col.ColumnName = "欄位名稱";
            col.DataType = typeof(string);
            dt.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "型別";
            col.DataType = typeof(string);
            dt.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "中文名稱";
            col.DataType = typeof(string);
            dt.Columns.Add(col);

            DataRow row = null;
            foreach (FieldSchema field in lstFields)
            {
                row = dt.NewRow();
                row["欄位名稱"] = field.ColumnName;
                row["型別"] = field.DataType.ToString();
                row["中文名稱"] = field.ColumnName;
                dt.Rows.Add(row);
            }

            dgvFields.DataSource = dt;
            
        }

        private void btnGen_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dgvFields.DataSource;
            foreach (DataRow row in dt.Rows)
            {
                //Log.D(row["CNAME"].ToString());
            }
        }

        
    }
}
